package com.canbot.u05.helper;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.canbot.u05.R;

/**
 * Created by zmp on 2019/4/4 17:36
 *
 * @author zmp
 */
public class DialogBuildHelper {

        public static class ITipClickListener {

                /**
                 * 取消.
                 */
                protected void cancel() {}

                /**
                 * 确定.
                 */
                protected void sure() {}

        }

        public interface IHintTipClickListener {

                /**
                 * 确定.
                 */
                void sure();

        }


        public static class ISetNameClickListener {

                /**
                 * 取消.
                 */
                protected void cancel() {}


                /**
                 * 确定.
                 *
                 * @param text        name
                 * @param tipTextView tipTextView
                 */
                protected void sure(String text, TextView tipTextView) {}

        }


        /**
         * 创建确认对话框
         *
         * @param context context
         * @return Dialog
         */
        public static TipBuilder createDialogTip(Context context) {
                final Dialog loadDialog = new Dialog(context, R.style.HostBaseDialogStyle);
                loadDialog.setContentView(R.layout.dialog_confirm_content);
                loadDialog.setCancelable(false);
                final Window window = loadDialog.getWindow();
                if (window != null) {
                        //                        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
                        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
                        window.getDecorView().setOnSystemUiVisibilityChangeListener(visibility -> {
                                int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                                        //布局位于状态栏下方
                                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                                        //全屏
                                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                                        //隐藏导航栏
                                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
                                uiOptions |= 0x00001000;
                                window.getDecorView().setSystemUiVisibility(uiOptions);
                        });
                }
                return new TipBuilder(loadDialog);
        }

        /**
         * 创建提示对话框
         *
         * @param context context
         * @return Dialog
         */
        public static HintTipBuilder createHintTipDialog(Context context) {
                Dialog switchTip = new Dialog(context, R.style.HostBaseDialogStyle);
                switchTip.setContentView(R.layout.dialog_guide_tip_content);
                switchTip.setCancelable(false);
                final Window window = switchTip.getWindow();
                if (window != null) {
                        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
                        window.getDecorView().setOnSystemUiVisibilityChangeListener(visibility -> {
                                int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                                        //布局位于状态栏下方
                                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                                        //全屏
                                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                                        //隐藏导航栏
                                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
                                uiOptions |= 0x00001000;
                                window.getDecorView().setSystemUiVisibility(uiOptions);
                        });
                }
                return new HintTipBuilder(switchTip);

        }

        /**
         * 创建确认对话框
         *
         * @param context context
         * @return Dialog
         */
        public static SetNameTipBuilder createDialogSetName(Context context) {
                View inflate = LayoutInflater.from(context).inflate(R.layout.dialog_set_name, (ViewGroup) null);
                final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context, R.style.MyDialogStyle2)
                        .setCancelable(false)
                        .setView(inflate);
                AlertDialog show = dialogBuilder.create();
                return new SetNameTipBuilder(show, inflate).show();
        }


        /**
         * 提示框辅助类.
         */
        public static class TipBuilder {

                private final Dialog mDialog;

                private TipBuilder(Dialog dialog) {
                        mDialog = dialog;
                }

                public TipBuilder setCancelable(boolean cancelable) {
                        mDialog.setCancelable(cancelable);
                        return this;
                }

                public TipBuilder setTitle(String title) {
                        ((TextView) mDialog.findViewById(R.id.titleView)).setText(title);
                        return this;
                }


                public TipBuilder setLeftButtonText(String leftText) {
                        ((Button) mDialog.findViewById(R.id.dialog_cancel)).setText(leftText);
                        return this;
                }

                public TipBuilder setRightButtonText(String rightButtonText) {
                        ((Button) mDialog.findViewById(R.id.dialog_sure)).setText(rightButtonText);
                        return this;
                }

                public TipBuilder setContent(String content, int gravity) {
                        TextView viewById = (TextView) mDialog.findViewById(R.id.contentView);
                        viewById.setGravity(gravity);
                        viewById.setText(content);
                        return this;
                }

                public TipBuilder setClickListener(ITipClickListener listener, boolean autoDismiss) {
                        mDialog.findViewById(R.id.dialog_cancel).setOnClickListener(v -> {
                                if (listener != null) {
                                        listener.cancel();
                                }
                                mDialog.dismiss();
                        });
                        mDialog.findViewById(R.id.dialog_sure).setOnClickListener(v -> {
                                if (listener != null) {
                                        listener.sure();
                                }
                                if (autoDismiss) {
                                        mDialog.dismiss();
                                }
                        });
                        return this;
                }

                public void dismiss() {
                        mDialog.dismiss();
                }

                public void show() {
                        mDialog.show();
                }

        }



        /**
         * 提示框辅助类.
         */
        public static class HintTipBuilder {

                private final Dialog mDialog;

                private HintTipBuilder(Dialog dialog) {
                        mDialog = dialog;
                }

                public HintTipBuilder setCancelable(boolean cancelable) {
                        mDialog.setCancelable(cancelable);
                        return this;
                }

                public HintTipBuilder setTitle(String title) {
                        ((TextView) mDialog.findViewById(R.id.titleView)).setText(title);
                        return this;
                }


                public HintTipBuilder setButtonText(String leftText) {
                        ((Button) mDialog.findViewById(R.id.bt_yes)).setText(leftText);
                        return this;
                }

                public HintTipBuilder setContent(String content, int gravity) {
                        TextView viewById = (TextView) mDialog.findViewById(R.id.contentView);
                        viewById.setGravity(gravity);
                        viewById.setText(content);
                        return this;
                }

                public HintTipBuilder setClickListener(IHintTipClickListener listener, boolean autoDismiss) {
                        mDialog.findViewById(R.id.bt_yes).setOnClickListener(v -> {
                                if (listener != null) {
                                        listener.sure();
                                }
                                if (autoDismiss) {
                                        mDialog.dismiss();
                                }
                        });
                        return this;
                }

                public void show() {
                        mDialog.show();
                }

                public void dismiss() {
                        mDialog.dismiss();
                }

        }



        /**
         * 任务名对话框辅助类.
         */
        public static class SetNameTipBuilder {

                private final Dialog mDialog;

                private final View mContentView;

                private final EditText editText;

                private final TextView setNameHintTv;

                private SetNameTipBuilder(Dialog dialog, View contentView) {
                        mDialog = dialog;
                        mContentView = contentView;
                        editText = (EditText) mContentView.findViewById(R.id.contentView);
                        setNameHintTv = (TextView) mContentView.findViewById(R.id.setNameHintTv);
                }

                public SetNameTipBuilder setCancelable(boolean cancelable) {
                        mDialog.setCancelable(cancelable);
                        return this;
                }

                public SetNameTipBuilder setTitle(String title) {
                        ((TextView) mContentView.findViewById(R.id.titleView)).setText(title);
                        return this;
                }


                public SetNameTipBuilder setLeftButtonText(String leftText) {
                        ((Button) mContentView.findViewById(R.id.dialog_cancel)).setText(leftText);
                        return this;
                }

                public SetNameTipBuilder setRightButtonText(String rightButtonText) {
                        ((Button) mContentView.findViewById(R.id.dialog_sure)).setText(rightButtonText);
                        return this;
                }

                public SetNameTipBuilder setContent(String content) {
                        ((EditText) mContentView.findViewById(R.id.contentView)).setText(content);
                        return this;
                }


                public SetNameTipBuilder setHintTextTv(String content, boolean isVisible) {
                        setNameHintTv.setText(content);
                        setNameHintTv.setVisibility(isVisible ? View.VISIBLE : View.INVISIBLE);
                        return this;
                }

                public SetNameTipBuilder setClickListener(ISetNameClickListener listener, boolean autoDismiss) {
                        mContentView.findViewById(R.id.dialog_cancel).setOnClickListener(v -> {
                                if (listener != null) {
                                        listener.cancel();
                                }
                                mDialog.dismiss();
                        });
                        mContentView.findViewById(R.id.dialog_sure).setOnClickListener(v -> {
                                if (listener != null) {
                                        String name = null;
                                        Editable text = editText.getText();
                                        if (text != null) {
                                                name = text.toString();
                                        }
                                        listener.sure(name, setNameHintTv);
                                }
                                if (autoDismiss) {
                                        mDialog.dismiss();
                                }
                        });
                        return this;
                }


                public SetNameTipBuilder show() {
                        mDialog.show();
                        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) mContentView.getLayoutParams();
                        layoutParams.width = (int) mContentView.getContext().getResources().getDimension(R.dimen.px880);
                        layoutParams.height = (int) mContentView.getContext().getResources().getDimension(R.dimen.px550);
                        layoutParams.gravity = Gravity.CENTER;
                        return this;
                }

                public void dismiss() {
                        mDialog.dismiss();
                }

        }

}
